package com.example.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;
import com.example.gulimall.product.dao.AttrDao;
import com.example.gulimall.product.dao.AttrGroupDao;
import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.example.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId
     * @return java.util.List<com.example.gulimall.product.vo.AttrGroupWithAttrsVo>
     * @author GuLin
     * @date 15:16 2021/6/8
     **/
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //根据分类ID查询分组信息
        List<AttrGroupEntity> entities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        if (null != entities && entities.size() > 0) {
            //2、查询所有属性
            List<AttrGroupWithAttrsVo> objectStream = entities.stream().map(item -> {
                AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
                BeanUtils.copyProperties(item, attrsVo);
                List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrsVo.getAttrGroupId());
                attrsVo.setAttrs(attrEntityList);
                return attrsVo;
            }).collect(Collectors.toList());
            return objectStream;
        }
        return null;
    }

    /**
     * 如果为0查询默认数据，否则根据分类ID查询关联属性分组信息
     *
     * @param params
     * @param catelogId
     * @return com.example.common.utils.PageUtils
     * @author GuLin
     * @date 14:51 2021/6/7
     **/
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //获取查询条件分组ID和分组名称
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            //使用and 括号括起来
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        IPage<AttrGroupEntity> page = null;
        if (0 == catelogId) {
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        } else {
            wrapper.eq("catelog_id", catelogId);
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        }
        return new PageUtils(page);
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
    }
}